exports.__esModule = true
module.exports= (function () {

  function Print (dom) {
    if (dom instanceof HTMLElement) {
      this.dom = dom
    } else if (typeof dom === 'string') {
      this.dom = document.querySelector(dom)
    } else {
      // TODO
    }
    this.init()
  }
  Print.prototype.id='';
  Print.prototype.init = function () {
    var style = this.getStyle()
    var html = this.dom.outerHTML
    var content = style + html
    this.id='print_'+parseInt(Math.random()*10000000).toString();
    this.iframeInit(content);
  }
  // 获取link 和 style
  Print.prototype.getStyle = function () {
    var styleString = ''
    var styles = document.querySelectorAll('style,link')
    styles.forEach(function (style) {
      styleString += style.outerHTML
    })
    return styleString
  }
  var _window=null;
  // 通过 iframe 打印
  Print.prototype.iframeInit = function (content) {

    var iframe=document.getElementById(this.id);
    
    if(iframe){
      _window=iframe.contentWindow;
      //_window.print()
    }else{
      iframe = document.createElement('iframe')
      iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-100%;left:-100%;')
      iframe.setAttribute('id',this.id);
      var el = document.body.appendChild(iframe)
      _window = el.contentWindow
      var _document = el.contentDocument || el.contentWindow.document
      _document.open()
      _document.write(content)
      _document.close()
      // iframe.onload = function () {
      //   _window.print()
      // }
    }
    Print.prototype.Start=function(){
      if(!_window){
        throw new Error('print content init failed.')
        return;
      }
      _window.print();

    }

    
    
    
  }
  return Print
}())